package com.liukuan.netty.bio2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by liukuan on 16/3/30.
 */
public class GetTimeHandler implements Runnable {

	private Socket socket;

	public GetTimeHandler(Socket socket) {
		this.socket = socket;
	}

	public void run() {
		try {
			InputStream in = socket.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String command = br.readLine();
			OutputStream out = socket.getOutputStream();
			String time = "bad query.";
			if (command.equals("time"))
				time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			out.write(time.getBytes());
			socket.shutdownOutput();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
